package de.bbl.android.adapter.GameCenter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.bbl.android.R;
import de.bbl.android.classes.Match;
import de.bbl.android.tools.Tools;

public class GameCenterArrayAdapter extends ArrayAdapter<Match> {

	private final Activity context;
	private final Match[] values;

	static class ViewHolder {
		public ImageView homeTeamImage;
		public TextView homeTeamName;
		public TextView DateOrPeriod;
		public TextView TimeOrScores;
		public TextView guestTeamName;
		public ImageView guestTeamImage;
	}
	
	public GameCenterArrayAdapter(Activity context, Match[] values) {
		super(context, R.layout.row_layout_gameday, android.R.id.text1, values);
		this.context = context;
		this.values = values;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.row_layout_gameday, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.homeTeamImage = (ImageView) rowView.findViewById(R.id.imageView1);
			viewHolder.homeTeamName = (TextView) rowView.findViewById(R.id.textView1); 
			viewHolder.DateOrPeriod = (TextView) rowView.findViewById(R.id.textView2);
			viewHolder.TimeOrScores = (TextView) rowView.findViewById(R.id.textView4);
			viewHolder.guestTeamName = (TextView) rowView.findViewById(R.id.textView3); 
			viewHolder.guestTeamImage = (ImageView) rowView.findViewById(R.id.imageView2);
			rowView.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		holder.homeTeamName.setText(values[position].getHomeTeam());
		holder.guestTeamName.setText(values[position].getAwayTeam());
		// Set Text Size
		if (values[position].getPeriod().toLowerCase().equals("prematch")) {
			holder.DateOrPeriod.setTextSize(12);
			holder.TimeOrScores.setTextSize(11);
			holder.DateOrPeriod.setText(values[position].getDate());
			holder.TimeOrScores.setText(values[position].getTime());
		} else {
			holder.DateOrPeriod.setTextSize(9);
			holder.TimeOrScores.setTextSize(14);
			holder.DateOrPeriod.setText(values[position].getPeriod());
			holder.TimeOrScores.setText(values[position].getScores());
		}
		holder.homeTeamImage.setImageResource(Tools.loadTeamImage(values[position].getHomeTeamId()));
		holder.guestTeamImage.setImageResource(Tools.loadTeamImage(values[position].getAwayTeamId()));
		
		return rowView;
	}
}
